// pages/home/stuHome/stuHome.js
import {
  formatTime
} from '../../../utils/date_formatter'
const appg = getApp().globalData
Component({

  /**
   * 页面的初始数据
   */
  data: {
    notice_type: {content:'服务器异常了，请检查'},
    threeNotice: [],
    fTime: null,
    calssList: [],
    userInfo: [],
  },
  properties: {},
  methods: {
    // 去公告详细页
    to_title_detail(e) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/subIndex/pages_first/title/detail_title/detail_title?id='+id,
      })
    },
    // 详细公告列表
    notice_more(e) {
      wx.navigateTo({
        url: '/subIndex/pages_first/title/title_list/title_list',
      })
    },
    // 去班级列表
    class_more() {
      wx.navigateTo({
        url: '/subIndex/pages_first/_class/class_list/class_list',
      })
    },
    // 去班级群聊界面
    to_group_chat(e) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/groupchat/groupchat/groupchat?id='+id,
      })
    },
    // 获取最新公告
    async getNewNotice() {
 
      try {
        const json = await getApp().$axios("/getNewNotice", "GET")
        if (json.status !== 200) {
          wx.showToast({
            title: json.data,
            icon: 'error'
          })
          return
        }

        this.setData({
          notice_type: json.data,

        })
      } catch (error) {
        wx.showToast({
          title: error.msg,
          icon: 'none'
        })
      }
    },
    // 获取最新三条公告
    async getNewThreeNotice() {
      try {
        const json = await getApp().$axios("/getThreeNotice", "GET")
        wx.hideLoading()
        if (json.status !== 200) {
          wx.showToast({
            title: json.data,
            icon: 'error'
          })
          return
        }
        // 处理时间
        const formatTimes = []
        json.data.forEach((item) => {
          formatTimes.push(formatTime(item.createdAt, "YYYY-MM-DD"))
        })
        this.setData({
          threeNotice: json.data,
          fTime: formatTimes
        })
        console.log(this.data);
      } catch (error) {
        wx.showToast({
          title: error.msg,
          icon: 'none'
        })
      }
    },
   // 获取班级列表
   async getClassList() {
    const page = {
      pageNum: 1,
      pageSize: 4,
      totalCount: 0
    }

    const uid = appg.userInfo.id;
    const userType = appg.userInfo.identity_ID
    try {
      const json = await getApp().$axios("/getGroupChatList", "POST", {
        uid,
        ...page,
        userType
      });
      console.log(json);
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      this.setData({
        calssList: json.data,
        userInfo: json.userInfo,
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      this.triggerEvent("EV_loading",{loading:true})
      this.getNewNotice()
      this.getNewThreeNotice()
      this.getClassList()
      this.triggerEvent("EV_loading",{loading:false})
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  // 组件在页面中的生命周期
  pageLifetimes: {
    show: function () {
      this.triggerEvent("EV_loading",{loading:true})
      // 页面被展示
      this.getNewNotice();
      this.getNewThreeNotice()
      this.getClassList()
      this.triggerEvent("EV_loading",{loading:false})
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  }
})